package com.lu.controller;

import com.lu.entity.User;
import com.lu.entity.UsernameAndPassword;
import com.lu.service.user.UserServices;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserServices userServices;

    //查询账号密码
    @GetMapping("/login")
    public List<UsernameAndPassword> getUser() {
        List<UsernameAndPassword> pwdAndUser = userServices.pwdAndUser();
        return pwdAndUser;
    }

    //插入数据
    @PostMapping("/insert")
    public void insert(@RequestBody User user) {
        userServices.insert(user);
    }

    //注册密码校验
    @PostMapping("/passwordCheck")
    public boolean passwordCheck(@RequestBody String password) {
        //前端传过来数据字符串最后一位字符是等于号，所以把最后一位字符截取了
        password = password.substring(0,password.length() - 1);
        //遍历字符数组取出数组里面的所有密码
        if (password.length() < 6) {
            return false;
        }
        return true;
    }

    //判断前端发来的用户名是否存在数据库中，存在返回true，反之false
    @PostMapping("/usernameCheck")
    public boolean usernameCheck(@RequestBody String username) throws UnsupportedEncodingException {
        //前端传过来数据字符串最后一位字符是等于号，所以把最后一位字符截取了
        username = username.substring(0,username.length() - 1);
        String[] mysqlUsername = userServices.username();
        String decodedUsername = URLDecoder.decode(username, "UTF-8");
        for (int i = 0; i < mysqlUsername.length; i++) {
            if (mysqlUsername[i].equals(decodedUsername)) {
                return true;
            }
        }
        return false;
    }

    //判断前端发来的邮箱是否存在数据库中，存在返回true，反之false
    @PostMapping("/emailCheck")
    public boolean emailCheck(@RequestBody String email) throws UnsupportedEncodingException {
        //前端传过来数据字符串最后一位字符是等于号，所以把最后一位字符截取了
        email = email.substring(0,email.length() - 1);
        //因为前端传过来的@符号在这被自动转成%40了，所有要将%40转换成@符号
        String decodedEmail = URLDecoder.decode(email, "UTF-8");
        String[] mysqlEmail = userServices.email();
        for (int i = 0; i < mysqlEmail.length; i++) {
            if (mysqlEmail[i].equals(decodedEmail)) {
                return true;
            }
        }
        return false;
    }

    //判断前端发来的账号密码和邮箱是否存在数据库中，存在返回true，反之false
    @PostMapping("/oldPasswordCheck")
    //判断用户名和密码，邮箱是否在数据库中，在true，则false
    public boolean oldPassword(@RequestBody List<User> oldPassword) {
        List<User> all = userServices.all();
        String password = "";
        String username = "";
        String email = "";
        //拿到前端的账号密码
        for (int i = 0; i < oldPassword.size(); i++) {
            User user = oldPassword.get(i);
            password = user.getPassword();
            username = user.getUsername();
            email = user.getEmail();
        }
        //拿到数据库中的账号密码并进行比较
        for (int i = 0; i < all.size(); i++) {
            User user = all.get(i);
            String mysqlPassword = user.getPassword();
            String mysqlUsername = user.getUsername();
            String mysqlEmail = user.getEmail();
            if (username.equals(mysqlUsername) && password.equals(mysqlPassword) && email.equals(mysqlEmail)) {
                return true;
            }
        }
        return false;
    }

    //修改密码
    @PutMapping("/updatePassword")
    public void updatePassword(@RequestBody User user) {
        log.info(String.valueOf(user));
        userServices.updatePassword(user);
    }
}
